/* $Id: js.js 65 2011-02-22 18:32:17Z jim2212001@gmail.com $ */
// JavaScript Document
var _errImg = 'site_media/images/pic-not-exist.png';
$(function() {
		$( "button, input:submit, input:button" ).button();
		resetTips();
});
if(!console){
  	var console = {
	  	log:function(obj){}
	};
}
function resetTips(){
	tips = $(".validateTips");
	$.each(tips,function(index,value){
		var obj=$(value);
		obj.text(obj.attr('title'));
	});
}
function updateTips(t){
  	tips
		.text( t )
		.addClass( "ui-state-highlight" );
	setTimeout(function() {
		tips.removeClass( "ui-state-highlight", 1500);
	}, 500 );
}
function updateObjTips(o,t){
	o.addClass("ui-state-error");
	updateTips(t);
}
function checkLength( o, n, min, max ) {
	if ( o.val().length > max || o.val().length < min ) {
		updateObjTips(o,
			n + " 字數必須介於 " +
			min + " 和 " + max + " 之間" );
		return false;
	} else {
		return true;
	}
}

function checkRegexp( o, regexp, n ) {
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass( "ui-state-error" );
		updateTips( n );
		return false;
	} else {
		return true;
	}
}

function infoMsg(msg){
	document.write('<div style="margin-top: 20px;" class="ui-state-highlight ui-corner-all"> \
				   <p><span class="ui-icon ui-icon-info" style="float: left; margin:0 .3em;"></span> \
				<strong>訊息:</strong> '+msg+'</p> \
				</div>');
}
function errMsg(msg){
	document.write('<div style="margin-top: 20px;" class="ui-state-error ui-corner-all"> \
				   <p><span class="ui-icon ui-icon-alert" style="float: left; margin:0 .3em;"></span> \
				<strong>警告:</strong> '+msg+'</p> \
				</div>');
}
function addBookList(listObj,bookjson,hasDeleteBtn,hasInput){
	var listdiv=listObj;
	var a=$('<a>').appendTo($('<h3>').appendTo(listdiv));
	a.text(bookjson.title);
	if(hasInput){
		console.log("book "+bookjson.id+" has value "+bookjson.value);
		a.html('<input name="book'+bookjson.id+'" class="text ui-coner-all ui-widget-content" value="'+(bookjson.value?bookjson.value:0)+'" /> '+a.text());
	}
	a.attr('href','#');
	var div=$('<div>').appendTo(listdiv)
	var ul=$('<ul>').appendTo(div);
	if(bookjson.thumb){
		var thumbLi=$('<li class="thumb">').appendTo(ul);
		var img=$('<img>').appendTo(thumbLi);
		img.attr('src',bookjson.thumb);
		$(img).error(function(){
		  	if($(this).attr('src')!=_errImg)
			  	$(this).attr('src',_errImg);
		});
	}
	if(bookjson.title){
		var titleLi=$('<li class="title">').appendTo(ul);
		titleLi.html('<strong>書名</strong> '+bookjson.title);
	}
	if(bookjson.author){
		var authorLi=$('<li class="author">').appendTo(ul);
		authorLi.html('<strong>作者</strong> '+bookjson.author);
	}
	if(bookjson.publisher){
		var publisherLi=$('<li class="publisher">').appendTo(ul);
		publisherLi.html('<strong>出版商</strong> '+bookjson.publisher);
	}
	if(bookjson.isbn){
		var isbnLi=$('<li class="isbn">').appendTo(ul);
		isbnLi.html('<strong>ISBN</strong> '+bookjson.isbn);
	}
	if(!isNaN(bookjson.price)){
		var priceLi=$('<li class="price">').appendTo(ul);
		priceLi.html('<strong>價錢</strong> '+bookjson.price);
	}
	if(bookjson.link){
		var linkLi=$('<li class="link">').appendTo(ul);
		linkLi.html('<a href="'+bookjson.link+'" target="_blank">連結</a>');
	}
	
	if(hasDeleteBtn){
		var deleteBtn=$('<button id="del'+bookjson.id+'" class="delete">').appendTo(div).button();
		deleteBtn.click(function(){
				if(confirm('確定刪除')){
					var bookId = this.id.split('del')[1];
					location.href='panel.php?page=editlist&action=deletebook&id='+$('#getid').val()+'&bookid='+bookId;
				}
			}
		);
		deleteBtn.text('刪除');
	}
}
function makeObjAccordion(obj){
	obj.accordion({
		autoHeight:false,
		change:function(event,ui){
				ui.newHeader.find('input').focus();
		}
	});
}
